<?php

declare(strict_types=1);

namespace app\common\logic;

use app\common\model\User as ModelUser;

class User
{
    /**
     * 添加、更新用户
     */
    public static function save(array $data)
    {
        $user = ModelUser::where('openid', $data['openid'])->findOrEmpty();
        $user->startTrans();
        try {
            $user->save($data);
            // 用户信息存入session
            session('wechat_user', $user->toArray());
            $user->commit();
        } catch (\Exception $e) {
            $user->rollback();
            fault($e->getMessage());
        }
    }
}
